package cn.yunyichina.provider.pay.pacity.service.impl;

import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.dto.pacity.PacityUserInfoRequestDTO;
import cn.yunyichina.provider.iface.entity.dto.pacity.PacityUserInfoResponseDTO;
import cn.yunyichina.provider.pay.pacity.constant.PacityConstant;
import cn.yunyichina.provider.pay.pacity.constant.ResponseEnum;
import cn.yunyichina.provider.pay.pacity.service.BaseService;
import cn.yunyichina.provider.pay.pacity.service.PacityUserService;
import cn.yunyichina.utils.commons.HttpContentType;
import cn.yunyichina.utils.commons.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created by Lullaby on 2017/6/16.
 */
@Service
public class PacityUserServiceImpl extends BaseService implements PacityUserService {

    private static final Logger logger = LoggerFactory.getLogger(PacityUserServiceImpl.class);

    /**
     * 获取平安平台用户信息
     *
     * @param requestDTO
     * @return
     */
    public ResCommon<PacityUserInfoResponseDTO> getPacityUserInfo(PacityUserInfoRequestDTO requestDTO) {
        try {
            String url = null;
            switch (PacityConstant.UserInfoScope.valueOf(requestDTO.getScope())) {
                case BASE:
                    url = getConfig(PacityConstant.PACITY_URL_USER_BASE);
                    break;
                case REAL:
                    url = getConfig(PacityConstant.PACITY_URL_USER_REAL);
                    break;
                case SICARD:
                    url = getConfig(PacityConstant.PACITY_URL_USER_SICARD);
                    break;
                default:
                    break;
            }
            logger.info("PacityUserServiceImpl.getPacityUserInfo.url -> {}", url);

            String postContent = JSON.toJSONString(requestDTO);
            logger.info("PacityUserServiceImpl.getPacityUserInfo.postContent -> {}", postContent);

            String postResponse = HttpUtil.httpBasicPostWithKeyValue(
                    PacityConstant.PACITY_USER_HTTP_BASIC_AUTH_KEY,
                    requestDTO.getAccessToken(),
                    "", url, HttpContentType.FORMURLENCODED
            );
            logger.info("PacityUserServiceImpl.getPacityUserInfo.postResponse -> {}", postResponse);

            PacityUserInfoResponseDTO userInfoResponse = JSON.parseObject(postResponse, PacityUserInfoResponseDTO.class);
            logger.info("PacityUserServiceImpl.getPacityUserInfo.userInfoResponse -> {}", JSON.toJSONString(userInfoResponse));

            if (userInfoResponse.isSuccess()) {
                return new ResCommon<PacityUserInfoResponseDTO>().buildResult(userInfoResponse);
            } else {
                return new ResCommon<PacityUserInfoResponseDTO>().buildResultCodeAndMessageAndResult(
                        ResponseEnum.PACITY_USER_INFO_FAILURE.getCode(),
                        ResponseEnum.PACITY_USER_INFO_FAILURE.getMessage(),
                        userInfoResponse
                );
            }
        } catch (Exception e) {
            logger.error("PacityUserServiceImpl.getPacityUserInfo.Exception", e);

            return new ResCommon<PacityUserInfoResponseDTO>().buildResultCodeAndMessage(
                    ResponseEnum.PACITY_USER_INFO_EXCEPTION.getCode(),
                    ResponseEnum.PACITY_USER_INFO_EXCEPTION.getMessage()
            );
        }
    }

}
